package com.cuitrip.business.home.my.card.presenter;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.app.MainApplication;
import com.cuitrip.app.base.UnitUtils;
import com.cuitrip.business.home.my.card.IPromotionView;
import com.cuitrip.business.home.recommend.model.ListResponse;
import com.cuitrip.business.pay.model.DiscountItem;
import com.cuitrip.component.item.CtTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.mvp.presenter.Presenter;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;

import java.util.List;

/**
 * Created by sheena on 16/2/27.
 */
public class PromotionPresenter implements Presenter<IPromotionView>,IProxyCallback {
    private ApiProxy mApiProxy=new ApiProxy(this);
    Activity mActivity;
    private CoordinatorLayout container;
    private CtTextView tvMsg;

    public PromotionPresenter(Activity activity) {
       mActivity=activity;
    }

    IPromotionView mPromotionView;
    @Override
    public void attachView(IPromotionView view) {
        mPromotionView=view;
    }

    @Override
    public void detachView() {
        this.mPromotionView=null;
    }

    public void renderUi(boolean mode){
        mPromotionView.displayLoading();
        if(mode){

        }

    }

    private void handleResponse(CtApiResponse response){
        if(response.isResponseNormal()){
            if(response.result instanceof ListResponse){
                List<DiscountItem> discountItems = ((ListResponse<DiscountItem>) response.result).getList();
                renderCouponList(discountItems);
            }
            mPromotionView.displayContent();
        }else{
            mPromotionView.displayNoNetwork();
        }
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if(ApiManager.VALID_COUPON.equals(response.getApiName())){
            handleResponse(response);
        }else if (ApiManager.SUBMIT_CODE.equals(response.getApiName())){
            if(response.isResponseNormal()){
                String msg=ResourceUtil.getString(R.string.operation_ok_2);
                showSnackBar(container,msg,true);
                requestData();
            }else{
                mPromotionView.applyFailed();
                String msg = response.msg;
                showSnackBar(container, msg,false);
                return false;
            }
        }
        return false;
    }


    public void requestData(){
        //查询已有优惠券信息
        OrderBusiness.getValidCoupon(mApiProxy, UnitUtils.getSettingMoneyType());
    }

    public void checkValidCode(String code, CoordinatorLayout cl, CtTextView tvMsg){
        //请求数据，校验code是否有效,如果有效，显示snackbar & 增加优惠券，如果无效，显示无效信息
        OrderBusiness.submitCode(mApiProxy, code);
        container = cl;
        this.tvMsg=tvMsg;
    }

    private void showSnackBar(CoordinatorLayout cl,String msg,boolean positive){
        Snackbar snackbar = Snackbar.make(cl, msg, Snackbar.LENGTH_SHORT);
        Snackbar.SnackbarLayout layoutView = (Snackbar.SnackbarLayout) snackbar.getView();
        TextView tv= (TextView) layoutView.findViewById(R.id.snackbar_text);
        tv.setTextColor(ResourceUtil.getColor(R.color.ct_white));
        Button btn= (Button) layoutView.findViewById(R.id.snackbar_action);
        btn.setTextColor(ResourceUtil.getColor(R.color.ct_white));

        //btn.setText(ResourceUtil.getString(R.string.order_selected_icon));
        if (positive){
            layoutView.setBackgroundColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
            btn.setText(R.string.order_selected_icon);
        }else {
            layoutView.setBackgroundColor(ResourceUtil.getColor(R.color.orange_ff6855));
            btn.setText(R.string.clear_icon);

        }
        btn.setTextSize(20.f);
        btn.setGravity(Gravity.RIGHT);
        btn.setTypeface(MainApplication.getIconFont());
        btn.setVisibility(View.VISIBLE);
        InputMethodManager ma = (InputMethodManager) mPromotionView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (mActivity.getCurrentFocus()!=null){
            ma.hideSoftInputFromWindow(mActivity.getCurrentFocus().getApplicationWindowToken(),0);
        }
        snackbar.show();
    }

    public void renderCouponList(List<DiscountItem> discountItems) {
        mPromotionView.renderCouponList(discountItems);
    }
}

